/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MemPane.java
 *
 * Created on Aug 21, 2009, 3:31:02 PM
 */
package org.dbdir.client.gui;

import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import org.dbdir.Client;

/**
 *
 * @author thotheolh
 */
public class MemPane extends javax.swing.JPanel {

    Client client;
    MainFrame frame;
    DefaultListModel model;

    /** Creates new form MemPane */
    public MemPane(Client client, MainFrame frame) {
        initComponents();
        this.client = client;
        this.frame = frame;
        this.setSize(332, 270);
    }

    public void init() {

        memList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        //memList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
        memList.setVisibleRowCount(-1);
        model = new DefaultListModel();
        memList.setModel(model);
        dbLbl.setText(client.getCurrentDBProfile());
        initData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bottomPane = new javax.swing.JPanel();
        leftPane = new javax.swing.JPanel();
        mainBtn = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        rightPane = new javax.swing.JPanel();
        rmBtn = new javax.swing.JButton();
        perBtn = new javax.swing.JButton();
        topPane = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        dbLbl = new javax.swing.JLabel();
        contentPane = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        memList = new javax.swing.JList();

        setLayout(new java.awt.BorderLayout());

        bottomPane.setLayout(new java.awt.BorderLayout());

        leftPane.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        mainBtn.setText("Main");
        mainBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mainBtnActionPerformed(evt);
            }
        });
        leftPane.add(mainBtn);

        jButton1.setText("DB Profile");
        jButton1.setToolTipText("DB Profile page");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        leftPane.add(jButton1);

        bottomPane.add(leftPane, java.awt.BorderLayout.WEST);

        rightPane.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        rmBtn.setText("Remove");
        rmBtn.setToolTipText("Remove member");
        rmBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rmBtnActionPerformed(evt);
            }
        });
        rightPane.add(rmBtn);

        perBtn.setText("Permission");
        perBtn.setToolTipText("Change Permission");
        perBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                perBtnActionPerformed(evt);
            }
        });
        rightPane.add(perBtn);

        bottomPane.add(rightPane, java.awt.BorderLayout.EAST);

        add(bottomPane, java.awt.BorderLayout.SOUTH);

        topPane.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel1.setText("DB Profile: ");
        topPane.add(jLabel1);

        dbLbl.setFont(new java.awt.Font("DejaVu Sans", 1, 13));
        topPane.add(dbLbl);

        add(topPane, java.awt.BorderLayout.NORTH);

        contentPane.setLayout(new java.awt.BorderLayout());

        memList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                memListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(memList);

        contentPane.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        add(contentPane, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void initData() {
        ArrayList userList = new ArrayList();
        model.removeAllElements();
        userList = client.getDBUserList(client.getCurrentDBProfile());
        if (userList != null && (!userList.isEmpty())) {
            for (int i = 0; i < userList.size(); i++) {
                model.addElement(userList.get(i).toString());
            }
        } else {
        }
    }

    private void mainBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mainBtnActionPerformed
        // TODO add your handling code here:
        frame.getActionPane();
}//GEN-LAST:event_mainBtnActionPerformed

    private void rmBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rmBtnActionPerformed
        // TODO add your handling code here:
        if (!memList.getValueIsAdjusting()) {
            if (memList.getSelectedIndex() != -1) {
                String rmusr = memList.getSelectedValue().toString();
                if (client.removeUser(rmusr, client.getCurrentDBProfile())) {
                    JOptionPane.showMessageDialog(this, rmusr + " have been successfully removed.", "User Removed", JOptionPane.INFORMATION_MESSAGE);
                    initData();
                } else {
                    JOptionPane.showMessageDialog(this, rmusr + " have failed to be removed.", "User Failed to be Removed", JOptionPane.ERROR_MESSAGE);
                }
            }else{
            }
        }

    }//GEN-LAST:event_rmBtnActionPerformed

    private void memListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_memListValueChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_memListValueChanged

    private void perBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_perBtnActionPerformed
        // TODO add your handling code here:
        if (!memList.getValueIsAdjusting()) {
            if (memList.getSelectedIndex() != -1) {
                String usr = memList.getSelectedValue().toString();
                client.setAccStr("permusr="+usr);
                frame.getPermPane();
            }else{
            }
        }
    }//GEN-LAST:event_perBtnActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        frame.getDBPane();
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel bottomPane;
    private javax.swing.JPanel contentPane;
    private javax.swing.JLabel dbLbl;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel leftPane;
    private javax.swing.JButton mainBtn;
    private javax.swing.JList memList;
    private javax.swing.JButton perBtn;
    private javax.swing.JPanel rightPane;
    private javax.swing.JButton rmBtn;
    private javax.swing.JPanel topPane;
    // End of variables declaration//GEN-END:variables
}
